package com.example.demo.web.dao;

import java.util.List;
import java.util.UUID;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.demo.web.Entity.UserRoleEntity;
import com.example.demo.web.util.XinLongUtil;


@Repository
public class UserRoleDAO {
	private Logger log = LogManager.getLogger(UserRoleDAO.class);
	private JdbcTemplate jdbcTemplate;

    @Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
    
    public int updateuserRole(UserRoleEntity userRoleEntity){

		// update
		String sql = "UPDATE tbl_user_role SET Createtime=?, Updatetime=?, "
					+ "Create_check_username=?, Createname=?, Update_check_username=?,Descriptor=?,Updatename=?,role_id=?,user_id=? WHERE id=?";
		try{
			jdbcTemplate.update(sql, userRoleEntity.getCreatetime(), userRoleEntity.getUpdatetime(),
					userRoleEntity.getCreate_check_username(), userRoleEntity.getCreatename(),userRoleEntity.getUpdate_check_username(),
					userRoleEntity.getDescriptor(),userRoleEntity.getUpdatename(),userRoleEntity.getRole_id(),userRoleEntity.getUser_id(),userRoleEntity.getId());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}
		
    	
    };
    
    public int saveuser(UserRoleEntity userRoleEntity){
    	String uId = UUID.randomUUID().toString().replace("-", "");
		// insert
		String sql = "INSERT INTO tbl_user_role (id,Createtime, Updatetime, Create_check_username,Createname,Update_check_username,Descriptor,Updatename,role_id,user_id)"
                + " VALUES (?, ?, ?, ?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(sql, uId,userRoleEntity.getCreatetime(), userRoleEntity.getUpdatetime(),
					userRoleEntity.getCreate_check_username(), userRoleEntity.getCreatename(),userRoleEntity.getUpdate_check_username(),
					userRoleEntity.getDescriptor(),userRoleEntity.getUpdatename(),userRoleEntity.getRole_id(),userRoleEntity.getUser_id());
			return 1;
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}
    	
    	
    	
    };
    
    public UserRoleEntity findByUserId(String Id) throws EmptyResultDataAccessException{
    	String sql = "SELECT * FROM tbl_user_role WHERE user_id='" + Id + "'";
    	try{
        	return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(UserRoleEntity.class));
    	}catch(Exception e){
    		XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
    		return null;
    	}    	
    };
    
    public List<UserRoleEntity> findByRoleId(String Id){
    	String sql = "SELECT * FROM tbl_user_role WHERE role_id='" + Id + "'";
    	try{
        	return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(UserRoleEntity.class));
    	}catch(Exception e){
    		XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
    		return null;
    	}    	
    }
    
    public int deleteByuserid(String userid) {
        String sql = "DELETE FROM tbl_user_role WHERE user_id=?";
        try{
        	return jdbcTemplate.update(sql, userid);
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return -1;
        }
        
    }
}
